package com.example.osixaffairssystem.service.impl;

import com.example.osixaffairssystem.domain.PageBean;
import com.example.osixaffairssystem.domain.model.Score;
import com.example.osixaffairssystem.mapper.ScoreMapper;
import com.example.osixaffairssystem.service.ScoreService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@RequiredArgsConstructor
public class ScoreServiceImpl implements ScoreService {

    final ScoreMapper scoreMapper;

    @Override
    public List<Score> list() {
        return scoreMapper.list();
    }

    @Override
    public PageBean<Score> listToPage(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        Page<Score> p = (Page<Score>) scoreMapper.list();
        return new PageBean<>(p.getTotal(), p.getResult());
    }

    @Override
    public Integer insert(Score score) {
        if (score.getScore() != 0) {
            if (score.getScore() >= 60) {
                score.setGrade(2);
            } else {
                score.setGrade(1);
            }
        }
        scoreMapper.insert(score);
        return score.getId();
    }

    @Override
    public void update(Score score) {
        if (score.getScore() != 0) {
            if (score.getScore() >= 60) {
                score.setGrade(2);
            } else {
                score.setGrade(1);
            }
        }
        scoreMapper.update(score);
    }

    @Override
    public void delete(List<Integer> ids) {
        scoreMapper.delete(ids);
    }
}
